package com.nut.web;

import com.nut.common.Result;
import com.nut.pojo.Book;
import com.nut.pojo.Page;
import com.nut.service.BookService;
import com.nut.service.impl.BookServiceImpl;
import com.nut.utils.JsonUtils;
import com.nut.utils.WebUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ClientServlet extends BaseServlet{
    private BookService bookService = new BookServiceImpl();

    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、获取请求的参数pageNo,pageSize
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"),1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //2、调用BookService.page()方法
        Page<Book> page = bookService.page(pageNo,pageSize);
        page.setUrl("client/clientServlet?action=page");
        req.setAttribute("page",page);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("page",page);
        Gson gson = new Gson();
        String json = gson.toJson(resultMap);
        resp.getWriter().write(json);
    }
}
